package com.ruoyi.outcome.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.outcome.domain.vo.ResearchProductTrack;
import com.ruoyi.outcome.domain.vo.ResearchTechReportTrack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.outcome.domain.ResearchTechReport;
import com.ruoyi.outcome.service.IResearchTechReportService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 技术报告管理Controller
 * 
 * @author zxh
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/report")
public class ResearchTechReportController extends BaseController
{
    @Autowired
    private IResearchTechReportService researchTechReportService;

    /**
     * 查询技术报告管理列表
     */
    @RequiresPermissions("outcome:report:list")
    @GetMapping("/list")
    public TableDataInfo list(ResearchTechReport researchTechReport)
    {
        startPage();
        researchTechReport.setIsDeleted(0);
        List<ResearchTechReport> list = researchTechReportService.selectResearchTechReportList(researchTechReport);
        return getDataTable(list);
    }

    /**
     * 导出技术报告管理列表
     */
    @RequiresPermissions("outcome:report:export")
    @Log(title = "技术报告管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchTechReport researchTechReport)
    {
        List<ResearchTechReport> list = researchTechReportService.selectResearchTechReportList(researchTechReport);
        ExcelUtil<ResearchTechReport> util = new ExcelUtil<ResearchTechReport>(ResearchTechReport.class);
        util.exportExcel(response, list, "技术报告管理数据");
    }

    /**
     * 获取技术报告管理详细信息
     */
    @RequiresPermissions("outcome:report:query")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId)
    {
        return success(researchTechReportService.selectResearchTechReportByReportId(reportId));
    }

    /**
     * 新增技术报告管理
     */
    @RequiresPermissions("outcome:report:add")
    @Log(title = "技术报告管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResearchTechReport researchTechReport)
    {
        researchTechReport.setCreatedBy(SecurityUtils.getUsername());

        ResearchTechReportTrack techReportTrack=new ResearchTechReportTrack();
        BeanUtils.copyProperties(researchTechReport, techReportTrack);
        techReportTrack.setActionType("ADD");
        researchTechReportService.insertResearchTechAudit(techReportTrack);
        return toAjax(researchTechReportService.insertResearchTechReport(researchTechReport));
    }

    /**
     * 修改技术报告管理
     */
    @RequiresPermissions("outcome:report:edit")
    @Log(title = "技术报告管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResearchTechReport researchTechReport)
    {
        researchTechReport.setUpdatedBy(SecurityUtils.getUsername());

        researchTechReport.setUpdatedAt(new Date());

        ResearchTechReportTrack techReportTrack=new ResearchTechReportTrack();
        BeanUtils.copyProperties(researchTechReport, techReportTrack);
        techReportTrack.setActionType("UPDATE");
        researchTechReportService.insertResearchTechAudit(techReportTrack);
        return toAjax(researchTechReportService.updateResearchTechReport(researchTechReport));
    }

    /**
     * 删除技术报告管理
     */
    @RequiresPermissions("outcome:report:remove")
    @Log(title = "技术报告管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        for(Long reportId:reportIds)
        {
            ResearchTechReport researchTechReport = researchTechReportService.selectResearchTechReportByReportId(reportId);
            researchTechReport.setDeletedBy(SecurityUtils.getUsername());
            researchTechReport.setDeletedAt(new Date());
            researchTechReport.setIsDeleted(1);
            researchTechReportService.updateResearchTechReport(researchTechReport);

            ResearchTechReportTrack techReportTrack=new ResearchTechReportTrack();
            BeanUtils.copyProperties(researchTechReport, techReportTrack);
            techReportTrack.setActionType("DELETE");
            researchTechReportService.insertResearchTechAudit(techReportTrack);
        }
        return success();
        //return toAjax(researchTechReportService.deleteResearchTechReportByReportIds(reportIds));
    }

    @GetMapping("/report/{reportId}")
    public AjaxResult getauditInfo(@PathVariable("reportId") Long reportId)
    {
        return success(researchTechReportService.selectResearchTechAuditList(reportId));
    }
}
